package com.hollycrm.hollysqm.job.index;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.lang.StringUtils;

import com.hollycrm.hollysqm.core.voice.bean.ParseResult;
import com.hollycrm.hollysqm.core.voice.parsing.IVoiceParse;
import com.hollycrm.hollysqm.core.voice.parsing.factory.VoiceParserFactory;

public class Main {

	public static void main(String[] args) {
		//8993125449614393BA40F2F4E139D533.xml
//		String filePath = "D:\\Workspaces\\MyEclipse10\\FTP_BAK\\172.16.52.4\\20170314_85_xml\\2017031423\\";		
//		Collection<File> cfs = getFileList(filePath);
//		for (File file : cfs) {
////			System.out.println(file.getPath());
//			readXml(file.getPath());
//		}
//		System.out.println("共："+cfs.size());
		
		
		String tempFile = "D:\\temp\\hollysqm\\xml2\\2017031422\\8993125449614393BA40F2F4E139D533.xml";
		readXml(tempFile);
	}

	/**
	 * 解析XML
	 * @param tempFile
	 */
	public static void readXml(String tempFile) {
//		String tempFile = "D:\\temp\\hollysqm\\xml\\2017030100\\6B9C1052BD6E4C89A4620F8392207A01.xml";
		ParseResult xmlBean = null;
		InputStream tempIs = null;
		String text="";
		try {
			tempIs = new FileInputStream(tempFile);
			IVoiceParse xmlParser = VoiceParserFactory.getInstance().defaultParser(tempIs, false);
			xmlBean = xmlParser.parse();
			text = xmlBean.getAllText();
			if (StringUtils.isNotBlank(text)){
				System.out.println(text);
			}else
				System.out.println("空："+tempFile);
		} catch (Exception e) {
			System.out.println("失敗的XML："+tempFile);
			e.printStackTrace();
		} finally {
			try {
				if (tempIs != null) {
					tempIs.close();
				}
			} catch (Exception e) {
			}
		}
	}
	
	/**
	 * 讀取目錄中所有XML
	 * @param filePath
	 * @return
	 */
	public static Collection<File> getFileList(String filePath) {
		Collection<File> cfs = null;
		try {
			cfs = FileUtils.listFiles(new File(filePath), FileFilterUtils.suffixFileFilter("xml"), null);
//			for (File file : cfs) {
//				System.out.println(file.getPath());
//			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return cfs;
		}
	}

}
